//
//  UIColor + Extension.swift
//  songguo
//
//  Created by 潘传洲 on 16/6/3.
//  Copyright © 2016年 pcz. All rights reserved.
//

import UIKit

//MARK:- 全局变量
let globalColor = UIColor.color(241, g: 99, b: 65)

// MARK: - 颜色的分类
extension UIColor{

    /// 随机色
    var randomColor:UIColor {
        let r = CGFloat(arc4random_uniform(255))
        let g = CGFloat(arc4random_uniform(255))
        let b = CGFloat(arc4random_uniform(255))
        let alp = CGFloat(arc4random_uniform(100))
        return UIColor(red: r/256, green: g/256, blue: b/256, alpha: alp/100)
    }
    
    /**
     根据RGB生成颜色
     
     - parameter r: 红色
     - parameter g: 绿色
     - parameter b: 蓝色
     
     - returns: 生成的颜色
     */
    class func color(r: Int, g: Int, b: Int) -> UIColor {
        return UIColor(red: CGFloat(r)/255.0, green: CGFloat(g)/255.0, blue: CGFloat(b)/255.0, alpha: 1.0)
    }
}

// MARK: - 根据颜色创建一张图片
extension UIImage{
    /**
     根据颜色创建一张图片
     
     - parameter color: 颜色
     
     - returns: 输出的图片
     */
    class func image(color: UIColor) -> UIImage {
        // 描述矩形
        let rect = CGRectMake(0.0, 0.0, 1.0, 1.0);
        // 开启位图上下文
        UIGraphicsBeginImageContext(rect.size);
        // 获取位图上下文
        let context = UIGraphicsGetCurrentContext();
        // 使用color演示填充上下文
        CGContextSetFillColorWithColor(context, color.CGColor);
        // 渲染上下文
        CGContextFillRect(context, rect);
        // 从上下文中获取图片
         let theImage = UIGraphicsGetImageFromCurrentImageContext();
        // 结束上下文
        UIGraphicsEndImageContext();
        
        return theImage;
    }
}

// MARK: - UIView的分类
extension UIView {
    public var x: CGFloat{
        get{
            return self.frame.origin.x
        }
        set{
            var r = self.frame
            r.origin.x = newValue
            self.frame = r
        }
    }
    
    public var y: CGFloat{
        get{
            return self.frame.origin.y
        }
        set{
            var r = self.frame
            r.origin.y = newValue
            self.frame = r
        }
    }
    /// 右边界的x值
    public var rightX: CGFloat{
        get{
            return self.x + self.width
        }
        set{
            var r = self.frame
            r.origin.x = newValue - frame.size.width
            self.frame = r
        }
    }
    /// 下边界的y值
    public var bottomY: CGFloat{
        get{
            return self.y + self.height
        }
        set{
            var r = self.frame
            r.origin.y = newValue - frame.size.height
            self.frame = r
        }
    }
    
    public var centerX : CGFloat{
        get{
            return self.center.x
        }
        set{
            self.center = CGPoint(x: newValue, y: self.center.y)
        }
    }
    
    public var centerY : CGFloat{
        get{
            return self.center.y
        }
        set{
            self.center = CGPoint(x: self.center.x, y: newValue)
        }
    }
    
    public var width: CGFloat{
        get{
            return self.frame.size.width
        }
        set{
            var r = self.frame
            r.size.width = newValue
            self.frame = r
        }
    }
    public var height: CGFloat{
        get{
            return self.frame.size.height
        }
        set{
            var r = self.frame
            r.size.height = newValue
            self.frame = r
        }
    }
    
    
    public var origin: CGPoint{
        get{
            return self.frame.origin
        }
        set{
            self.x = newValue.x
            self.y = newValue.y
        }
    }
    
    public var size: CGSize{
        get{
            return self.frame.size
        }
        set{
            self.width = newValue.width
            self.height = newValue.height
        }
    }
    
}